package com.liu.seckill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.seckill.exception.GlobalException;
import com.liu.seckill.mapper.OrderMapper;
import com.liu.seckill.pojo.Order;
import com.liu.seckill.pojo.SeckillGoods;
import com.liu.seckill.pojo.SeckillOrder;
import com.liu.seckill.pojo.User;
import com.liu.seckill.service.IGoodsService;
import com.liu.seckill.service.IOrderService;
import com.liu.seckill.service.ISeckillGoodsService;
import com.liu.seckill.service.ISeckillOrderService;
import com.liu.seckill.util.JsonUtil;
import com.liu.seckill.vo.GoodsVo;
import com.liu.seckill.vo.OrderDetailVo;
import com.liu.seckill.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @ClassName OrderServiceImpl
 * @Author ：name
 * @Date ：2022-02-16 14:37
 * @Description：
 * @Version: 1.0
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {


    @Autowired
    private ISeckillGoodsService seckillGoodsService;

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    @Transactional
    public Order seckill(User user, GoodsVo goods) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //秒杀商品表减库存
        SeckillGoods seckillGoods = seckillGoodsService.getOne(new
                QueryWrapper<SeckillGoods>().eq("goods_id",
                    goods.getId()));

   boolean seckillGoodsResult = seckillGoodsService.update(
                    new UpdateWrapper<SeckillGoods>().setSql("stock_count = stock_count- 1").eq("goods_id", goods.getId()).gt("stock_count", 0));
                 // seckillGoodsService.updateById(seckillGoods);
                 if (seckillGoods.getStockCount() < 1) {
   //判断是否还有库存
   valueOperations.set("isStockEmpty:" + goods.getId(), "0");
   return null;
  }

        //生成订单
       Order order = new Order();
       order.setUserId(user.getId());
       order.setGoodsId(goods.getId());
       order.setDeliveryAddrId(0L);
       order.setGoodsName(goods.getGoodsName());
       order.setGoodsCount(1);
       order.setGoodsPrice(seckillGoods.getSeckillPrice());
       order.setOrderChannel(1);
       order.setStatus(0);
       order.setCreateDate(new Date());
       orderMapper.insert(order);
       //生成秒杀订单
       SeckillOrder seckillOrder = new SeckillOrder();
       seckillOrder.setOrderId(order.getId());
       seckillOrder.setUserId(user.getId());
       seckillOrder.setGoodsId(goods.getId());
       seckillOrderService.save(seckillOrder);
        redisTemplate.opsForValue().set("order:" + user.getId() + ":" +
                        goods.getId(),JsonUtil.object2JsonStr(seckillOrder));
       return order;

    }

    @Override
    public OrderDetailVo detail(Long orderId) {
        if(null==orderId){
            throw new GlobalException(RespBeanEnum.ORDER_NOT_EXIST);
        }
        Order order=orderMapper.selectById(orderId);
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(order.getGoodsId());
        OrderDetailVo detail = new OrderDetailVo();
        detail.setGoodsVo(goodsVo);
        detail.setOrder(order);
        return detail;
    }
}
